package com.crm.common.ka.core.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BiUtils {
    /**
     * 计算转化率
     *
     * @param count      有效的数量
     * @param totalCount 总数
     * @return 转化率字符串
     */
    public static String calculateRate(Integer count, Integer totalCount) {

        BigDecimal valid = new BigDecimal(count.toString());
        BigDecimal total = new BigDecimal(totalCount.toString());

        BigDecimal rate;
        if (totalCount == 0) {
            rate = valid.divide(new BigDecimal("1"), 2, RoundingMode.HALF_UP);
        } else {
            rate = valid.divide(total, 4, RoundingMode.HALF_UP);
        }

        return rate.multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP) + "%";
    }

}
